package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        // 0.先查出总的订单数(后面计算有效订单要用)
        Integer orderCount = orderMapper.countOrdersByMap(map);
        // 1.查询出新增用户数量
        Integer newUser = userMapper.countByMap(map);
        map.put("status", Orders.COMPLETED);
        // 2.查询当天营业额
        Double turnover = orderMapper.getSumByMap(map);
        // 3.查询有效订单
        Integer validOrderCount = orderMapper.countOrdersByMap(map);
        // 4.查询订单完成率与平均客单价
        Double orderCompletionRate = 0.0;
        Double unitPrice = 0.0;
        // 总订单有时候会是0，为了防止异常，所以要判断总订单是否为0
        DecimalFormat df = new DecimalFormat("0.0");
        if(orderCount != 0 && validOrderCount != 0){
            // 订单完成率
            orderCompletionRate = Double.valueOf(df.format((float)validOrderCount / orderCount));
            // 平均客单价
            unitPrice = turnover / validOrderCount;
        }

        // 5.将上述查出来的5个属性封装进BusinessDataVO中
        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUser)
                .build();

        return businessDataVO;
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewSetMeals() {
        // 1.查询起售状态的套餐数量
        Integer sold = setmealMapper.getOverviewSetMealsByStatus(StatusConstant.ENABLE);

        // 2.查询停售状态的套餐数量
        Integer discontinued = setmealMapper.getOverviewSetMealsByStatus(StatusConstant.DISABLE);

        // 3.将上述两个属性封装进SetmealOverViewVO中
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();

        return setmealOverViewVO;
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
        // 1.查询起售状态的菜品数量
        Integer sold = dishMapper.getOverviewDishesByStatus(StatusConstant.ENABLE);

        // 2.查询停售状态的套餐数量
        Integer discontinued = dishMapper.getOverviewDishesByStatus(StatusConstant.DISABLE);

        // 3.将上述两个属性封装进DishOverViewVO中
        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();

        return dishOverViewVO;
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
        // 1.查询订单状态为待接单的数量
        Integer waitingOrders = orderMapper.getOrderSumByStatus(Orders.TO_BE_CONFIRMED);
        // 2.查询订单状态为待派送的数量
        Integer deliveredOrders = orderMapper.getOrderSumByStatus(Orders.CONFIRMED);
        // 3.查询订单状态为已完成的数量
        Integer completedOrders = orderMapper.getOrderSumByStatus(Orders.COMPLETED);
        // 4.查询订单状态为已取消的数量
        Integer cancelledOrders = orderMapper.getOrderSumByStatus(Orders.CANCELLED);
        // 5.查询所有订单的数量
        Integer allOrders = orderMapper.countOrdersByMap(null);

        // 6.将上述查询出的5个属性封装进OrderOverViewVO中
        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();

        return orderOverViewVO;
    }
}
